package com.framework.auth.util;

import com.framework.auth.translator.AuthenticationResultHandler;
import org.springframework.security.oauth2.server.authorization.web.OAuth2TokenEndpointFilter;
import org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationFilter;
import org.springframework.security.web.SecurityFilterChain;

public class AuthenticationHandlerUtils {
    public static void resetErrorHandler(SecurityFilterChain chain, AuthenticationResultHandler resultHandler) {
        chain.getFilters()
                .forEach(it -> {
                    if (it instanceof BearerTokenAuthenticationFilter) {
                        ((BearerTokenAuthenticationFilter) it).setAuthenticationFailureHandler(resultHandler);
                    }
                    if (it instanceof OAuth2TokenEndpointFilter) {
                        ((OAuth2TokenEndpointFilter) it).setAuthenticationFailureHandler(resultHandler);
                    }
                });
    }
}
